package com.wanli.agent.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wanli.agent.R;
import com.wanli.agent.login.LoginActivity;
import com.wanli.agent.mine.UserAgreementActivity;
import com.wanli.agent.utils.Dp2Px;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.utils.ScreenUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Author:HWC
 */
public class PrivacyDialog extends DialogFragment {


    @BindView(R.id.dialog_message)
    TextView dialogMessage;
    private Dialog dialog;
    Unbinder unbinder;
    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_privacy, null);
        unbinder = ButterKnife.bind(this, view);
        mContext = getActivity();
        initView();
        return view;
    }

    public static PrivacyDialog newInstance() {
        PrivacyDialog fragment = new PrivacyDialog();
        return fragment;
    }

    //    初始化请求
    private void initView() {
        String str = getResources().getString(R.string.privacy);
        SpannableStringBuilder spannable = new SpannableStringBuilder(str);
        spannable.setSpan(new TextClick(1), 31, 37, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new TextClick(2), 38, 44, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        dialogMessage.setMovementMethod(LinkMovementMethod.getInstance());
        dialogMessage.setText(spannable);
    }

    private class TextClick extends ClickableSpan {
        private int type;

        public TextClick(int type) {
            this.type = type;
        }

        @Override
        public void onClick(View widget) {
            if (isAdded()) {
                avoidHintColor(widget);
            }
            Intent intent = new Intent(getActivity(), UserAgreementActivity.class);
            intent.putExtra("type", type);
            startActivity(intent);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(getResources().getColor(R.color.color_blue2));
            ds.setUnderlineText(false);
            ds.clearShadowLayer();
        }
    }

    private void avoidHintColor(View view) {
        if (view instanceof TextView)
            ((TextView) view).setHighlightColor(getResources().getColor(android.R.color.transparent));
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return true;
            }
        });
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (ScreenUtils.getScreenWidth(mContext) - Dp2Px.dp2Px(65));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.dialog_negative, R.id.dialog_positive})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_negative:
                dismiss();
                getActivity().finish();
                break;
            case R.id.dialog_positive:
                SPManager.getInstance().saveProtocol("true");
                dismiss();
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;
        }
    }
}
